package com.yuwei.goods_shopping.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.yuwei.goods_shopping.databinding.GooddsShoppingItemBinding
import com.yuwei.goods_shopping.entity.GoodsShoppingEntityItem
import com.yuwei.image.glide.GlideImageLoader

class GoodsShoppingAdapter constructor(private val list: MutableList<GoodsShoppingEntityItem>):RecyclerView.Adapter<GoodsShoppingAdapter.ItemViewHolder>() {
    private var itemClickListener: OnItemClickListener? = null
    private var isShow = false
    fun setIsShow(show:Boolean){
        this.isShow = show
    }
    inner class ItemViewHolder(val dataBinding:GooddsShoppingItemBinding):RecyclerView.ViewHolder(dataBinding.root)
    interface OnItemClickListener{
        fun onItemClick(v: View, position: Int)
    }
    fun setItemOnClickListener(l: OnItemClickListener){
        itemClickListener = l
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        return ItemViewHolder(GooddsShoppingItemBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }


    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val entity = list[position]
        holder.dataBinding.goodsCarTitle.text = entity.goods_desc
        GlideImageLoader.loadImage(holder.dataBinding.root.context,holder.dataBinding.goodsCarIv,entity.goods_default_icon)
        holder.dataBinding.goodsCarMoney.text = "￥${entity.goods_default_price}"
        holder.dataBinding.goodsCarNum.text = entity.count.toString()
        holder.dataBinding.goodsCarRvCheck.isChecked = entity.check
        if (isShow){
            holder.dataBinding.goodsCarDelete.visibility = View.VISIBLE
            holder.dataBinding.goodsCarBtnAdd.visibility = View.GONE
            holder.dataBinding.goodsCarNum.visibility = View.GONE
            holder.dataBinding.goodsCarBtnSubtract.visibility = View.GONE
        }else{
            holder.dataBinding.goodsCarDelete.visibility = View.GONE
            holder.dataBinding.goodsCarBtnAdd.visibility = View.VISIBLE
            holder.dataBinding.goodsCarNum.visibility = View.VISIBLE
            holder.dataBinding.goodsCarBtnSubtract.visibility = View.VISIBLE
        }
        //点击事件
        holder.dataBinding.goodsCarBtnAdd.setOnClickListener {
            itemClickListener?.onItemClick(it, position)
        }

        holder.dataBinding.goodsCarBtnSubtract.setOnClickListener {
            itemClickListener?.onItemClick(it, position)
        }

        holder.dataBinding.goodsCarRvCheck.setOnClickListener {
            itemClickListener?.onItemClick(it, position)
        }
        holder.dataBinding.goodsCarDelete.setOnClickListener {
            itemClickListener?.onItemClick(it,position)
        }



    }
}